window.eel = {
  _host: window.location.origin,

  set_host: function(hostname) {
    window.eel._host = hostname;
  },

  expose: function(f, name) {
    if (name === undefined) {
      name = f.toString();
      let i = 'function '.length,
        j = name.indexOf('(');
      name = name.substring(i, j).trim();
    }

    window.eel._exposed_functions[name] = f;
  },

  // These get dynamically added by library when file is served
  _py_functions: ['pick_file', 'get_image', 'getLog'],
  _start_geometry: { default: { size: [1280, 800], position: null }, pages: {} },

  _exposed_functions: {},

  _mock_queue: [],
  wsOpened: false,
  _mock_py_functions: function() {
    for (let i = 0; i < window.eel._py_functions.length; i++) {
      let name = window.eel._py_functions[i];
      eel[name] = function() {
        let call_object = window.eel._call_object(name, arguments);
        window.eel._mock_queue.push(call_object);
        return new Promise(function(resolve) {
          window.eel._call_return_callbacks[call_object.call] = resolve;
        });
      };
    }
  },

  _import_py_function: function(name) {
    let func_name = name;
    eel[name] = function() {
      let call_object = window.eel._call_object(func_name, arguments);
      window.eel._websocket.send(window.eel._toJSON(call_object));
      return new Promise(function(resolve) {
        window.eel._call_return_callbacks[call_object.call] = resolve;
      });
    };
  },

  run_py: function(obj_name, fun_name, args) {
    let arg_array = [];
    if (args) {
      for (let i = 0; i < args.length; i++) {
        arg_array.push(args[i]);
      }
    }
    let call_id = (eel._call_number += 1) + Math.random();
    let _run_py_call_object = {
      call: call_id,
      obj_name: obj_name,
      fun_name: fun_name,
      args: arg_array,
    };
    eel._websocket.send(eel._toJSON(_run_py_call_object));
    return new Promise(function(resolve) {
      eel._call_return_callbacks[_run_py_call_object.call] = resolve;
    });
  },
  _call_number: 0,

  _call_return_callbacks: {},

  _call_object: function(name, args) {
    let arg_array = [];
    for (let i = 0; i < args.length; i++) {
      arg_array.push(args[i]);
    }

    let call_id = (eel._call_number += 1) + Math.random();
    return { call: call_id, name: name, args: arg_array };
  },

  _sleep: function(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  },

  _toJSON: function(obj) {
    return JSON.stringify(obj, (k, v) => (v === undefined ? null : v));
  },

  _call_return: function(call) {
    return function(callback = null) {
      // console.log("callback:",callback);
      if (callback != null) {
        window.eel._call_return_callbacks[call.call] = callback;
      } else {
        // console.log("_call_return_callbacks:",window.eel._call_return_callbacks);
        return new Promise(function(resolve) {
          window.eel._call_return_callbacks[call.call] = resolve;
        });
      }
    };
  },

  _position_window: function(page) {
    let size = window.eel._start_geometry['default'].size;
    let position = window.eel._start_geometry['default'].position;

    if (page in window.eel._start_geometry.pages) {
      size = window.eel._start_geometry.pages[page].size;
      position = window.eel._start_geometry.pages[page].position;
    }

    if (size != null) {
      window.resizeTo(size[0], size[1]);
    }

    if (position != null) {
      window.moveTo(position[0], position[1]);
    }
  },
};
window.oncontextmenu = function(e) {
  //取消默认的浏览器自带右键 很重要！！
  e.preventDefault();
};
